﻿using System;
using System.Collections;
using System.Reflection;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Xml;
using VitCode.BaseDataPageCreator.Setting;

namespace VitCode.BaseDataPageCreator
{
    /// <summary>
    /// PageLoadHelper 的摘要說明。
    /// </summary>
    public class PageLoadHelper
    {
        public PageLoadHelper()
        {
            
        }

        public PageLoadHelper(string settingXmlPath)
        {
            m_XmlReader = new ObjXmlReader(settingXmlPath);
        }

        ObjXmlReader m_XmlReader;

        public ObjXmlReader XmlReader
        {
            get { return m_XmlReader; }
        }

        public string GetStringValue(Type valType,object valObject)
        {
            string strVal = "";
            if(valObject==null)
            {
                if(valType ==  typeof(Guid))
                    strVal = Guid.Empty.ToString();
                else if (valType.IsEnum || valType ==  typeof(Int16) || valType ==  typeof(Int16) || valType ==  typeof(Double) || valType ==  typeof(Int32) || valType ==  typeof(Int64)
                         || valType ==  typeof(Single))
                    strVal = "0";
            }
            else
                strVal = valObject.ToString();
            return strVal;
        }

        public string GetObjProStringValue(PropertyInfo pro,object obj)
        {
            string strVal = "";
            object proVal = pro.GetValue(obj, null);
            if(proVal==null)
            {
                if(pro.PropertyType ==  typeof(Guid))
                    strVal = Guid.Empty.ToString();
                else if (pro.PropertyType.IsEnum || pro.PropertyType ==  typeof(Int16) || pro.PropertyType ==  typeof(Int16) || pro.PropertyType ==  typeof(Double) || pro.PropertyType ==  typeof(Int32) || pro.PropertyType ==  typeof(Int64)
                         || pro.PropertyType ==  typeof(Single))
                    strVal = "0";
            }
            else
                strVal = proVal.ToString();
            return strVal;
        }

        public string GetProObjStringValue(string proName,object obj)
        {
            if(obj==null)
                return "";
            PropertyInfo pi = obj.GetType().GetProperty(proName);
            if(pi==null)
                return "";
            return GetObjProStringValue(pi, obj);
        }



        public string PROPERTY = "property", CTL_ATTR_INFO = "CtlAttrInfo", PAGE_RULE = "page_rule", CHECK_CTL_IDS = "CheckCtlIds", CTL_TYPE = "CtlType", PAGE_CAN_DELETE = "PageCanDelete", PAGE_CAN_ADD = "PageCanAdd", ISBINDDATA = "IsBindData", SHOW_SUCCESS_MESSAGE = "ShowSuccessMessage" ,GET_WEBSERVICE ="GetWebService";

        public string  SELECT_PAGE_URL = "SelectPageURL", MEMO="Memo",OBJ_PRO_HEAD_STRING="ObjPro_", PG_LIST_IS_SHOW_EDIT_INFO="PgListIsShowEditInfo";

        public string CTL_PROPERTY_FRO_OBJCET = "CtlPropertyFroObject", CTL_OBJ_KEY_PAO = "CtlObjKeyPro", CTL_OBJ_SHOW_PAO = "CtlObjShowPro", CTL_OBJ_SHOW_PAO_2 = "CtlObjShowPro2";

        public string KEY_CONTEXT = "KeyContext", KEY_PROPERTY = "KeyProperty",INIT_NOW="InitNow",CONDITION_STRING="ConditionString";

        public string INIT_CURR_USER_IFNO = "InitCurrUserInfo",OTHER_CTL_CONTEXT="OtherCtlContext";

        public string FORMAT_STRING="FormatString",ASSEMBLY_NAME="AssemblyName",NAME_SPACE="NameSpace",COMMAND_NAME="CommandName";

        public string NAME="Name", TYPE_NAME="TypeName",LIST_SQL="ListSql",SQL="sql",CLASS_NAME="ClassName",USER_CTL_PATH="UserCtlPath",CTL_PRO_INFO="CtlProInfo";

        public string LGNORE_SAVE = "LgnoreSave",TIMING_IS_USE_PRO_NAME="TimingIsUseProName", TIMING_IS_USE_CONDITION_INFO="TimingIsUseConditionInfo";

        public string GetAssemblyName(string className)
        {
            return  m_XmlReader.GetNodeAttributeValue(className, ASSEMBLY_NAME);
        }

        public Type GetClassTypeByClassNodeName(string classNodeName,string assemblyName,string nameSpace)
        {
            return Assembly.Load(assemblyName).GetType(nameSpace + "." + classNodeName);
        }

        public string GetClassKeyPropertyName(string className)
        {
            XmlNode classNode = m_XmlReader.GetClassXMLNode(className);
            if(classNode == null)
                return "";
            string keyProName = m_XmlReader.GetNodeAttributeValue(className, KEY_PROPERTY);
            return keyProName;
        }

        public Type GetType(string typeName)
        {
            string[] ss = typeName.Split(',');
            if(ss == null || ss.Length!=2)
                return null;
            string typeFullName = ss[0];
            string assemblyName = ss[1];
            Type val = Assembly.Load(assemblyName).GetType(typeFullName,false,false);
            return val;
        }

        public Type GetClassTypeByClassNodeName(string classNodeName)
        {
            XmlNode nd = m_XmlReader.GetClassXMLNode(classNodeName);
            string strAssName = m_XmlReader.GetNodeAttributeValue(nd, ASSEMBLY_NAME);
            string strNameSpace = m_XmlReader.GetNodeAttributeValue(nd, NAME_SPACE);
            return GetClassTypeByClassNodeName(classNodeName, strAssName, strNameSpace);
        }

        public void SetProperty(object o,string proName,object proValue)
        {
            if(o == null)
                return;
            PropertyInfo pi = o.GetType().GetProperty(proName);
            if(pi==null)
                return;
            string strProValue = GetStringValue(pi.PropertyType, proValue);
            pi.SetValue(o,Convert.ChangeType(strProValue,pi.PropertyType),null);
			
        }
        public PropertyInfo GetProperty(XmlNode proNode,string className)
        {
            string strName = proNode.Attributes[NAME].Value;
            return GetProperty(strName, className);
        }

        public PropertyInfo GetProperty(XmlNode proNode, Type objType)
        {
            string strName = proNode.Attributes[NAME].Value;
            return objType.GetProperty(strName);
        }



        public PropertyInfo GetProperty(XmlNode proNode, string className,string typeName)
        {
            string strName = proNode.Attributes[NAME].Value;
            return GetProperty(strName, className,typeName);
        }

        public PropertyInfo GetProperty(string proName,string className)
        {
            Type type = GetClassTypeByClassNodeName(className);
            PropertyInfo pi = type.GetProperty(proName);
            return pi;
        }

        public PropertyInfo GetProperty(string proName, string className, string typeName)
        {
            Type type = GetClassTypeByClassNodeName(className);
            //string strProName = m_XmlReader.GetNodeAttributeValue(className,typeName,pron);
            PropertyInfo pi = type.GetProperty(proName);
            return pi;
        }

        public object GetPropertyValue(string proName,object obj)
        {
            PropertyInfo p = GetProperty(proName, obj.GetType().Name);
            return p.GetValue(obj, null);
        }


        public string SetObjectKeyField(string className)
        {
            XmlNode classNode = m_XmlReader.GetClassXMLNode(className);
            return m_XmlReader.GetNodeAttributeValue(classNode, KEY_PROPERTY);
        }

        public XmlNodeList GetProNodesFromXML(string className,string typeName)
        {
            return m_XmlReader.GetChildNodesFromXML(className, typeName, "property");
        }

        public XmlNodeList GetProNodesFromXML(string className)
        {
            return m_XmlReader.GetChildNodesFromXML(className, "property");
        }


        public bool CompareUserControl(Type type,Type userControlType)
        {
            return type.Name == userControlType.Name+"_ascx";
        }

        public string REQUIED_TXT_IDS="page_rule_hdRequiedTxtIds";
		
        public void CreateCheckSettingHiddenCtl(Page page)
        {
            ArrayList al = new ArrayList();
            al.Add(new HtmlInputHidden());
            ((HtmlInputHidden) (al[0])).ID = REQUIED_TXT_IDS;
			
            foreach(HtmlInputHidden hd in al)
            {
                page.Controls.Add(hd);
            }
        }
    }
}
